import { ClrService } from "@/services/classroom";
import { ActionType } from "@ant-design/pro-components";
import { message } from "antd";
import { useCallback, useRef } from "react";

export default()=>{
    const actionRef = useRef<ActionType>();

    const getHandler = useCallback(async () => {
      return ClrService.getClrList();
    }, []);
    //处理删除
    const removeHandler = useCallback(async (id:number) => {
      const hide = message.loading('dismissing..');
      try {
        await ClrService.removeClr(id);
        hide();
        message.success(`删除成功！`);
        actionRef.current?.reloadAndRest?.();
        return true;
      } catch (error) {
        hide();
        message.error(`删除失败，请重试！`);
        return false;
      }
    }, []);
    
    
     const addHandler = useCallback(async (body: API.Clr_Add_Body) => {
       const hide = message.loading('dismissing..');
       try {
         await ClrService.addClr(body);
         hide();
         message.success(`新增成功！`);
         actionRef.current?.reload?.();
         return true;
       } catch (error) {
         hide();
         message.error(`新增失败，请重试！`);
         return false;
       }
     }, []);
     const updateHandler = useCallback(async (body: API.Clr_Update_Body) => {
       const hide = message.loading('dismissing..');
       try {
         await ClrService.updateClr(body);
         hide();
         message.success(`修改成功！`);
         actionRef.current?.reload?.();
         return true;
       } catch (error) {
         hide();
         message.error(`修改失败，请重试！`);
         return false;
       }
     }, []);
    return {
      actionRef,
      getHandler,
      removeHandler,
      addHandler,
      updateHandler,
    };
}